﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace WebHelper.Helpers
{
    public class MimeTypeHelper
    {
        [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
        private extern static System.UInt32 FindMimeFromData(
            System.UInt32 pBC,
            [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
            [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
            System.UInt32 cbSize,
            [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
            System.UInt32 dwMimeFlags,
            out System.UInt32 ppwzMimeOut,
            System.UInt32 dwReserverd
        );

        private static Dictionary<string, string> mime = new Dictionary<string, string>();
        static MimeTypeHelper()
        {
            mime.Add("323", "text/h323");
            mime.Add("aaf", "application/octet-stream");
            mime.Add("aca", "application/octet-stream");
            mime.Add("accdb", "application/msaccess");
            mime.Add("accde", "application/msaccess");
            mime.Add("accdt", "application/msaccess");
            mime.Add("acx", "application/internet-property-stream");
            mime.Add("afm", "application/octet-stream");
            mime.Add("ai", "application/postscript");
            mime.Add("aif", "audio/x-aiff");
            mime.Add("aifc", "audio/aiff");
            mime.Add("aiff", "audio/aiff");
            mime.Add("application", "application/x-ms-application");
            mime.Add("art", "image/x-jg");
            mime.Add("asd", "application/octet-stream");
            mime.Add("asf", "video/x-ms-asf");
            mime.Add("asi", "application/octet-stream");
            mime.Add("asm", "text/plain");
            mime.Add("asr", "video/x-ms-asf");
            mime.Add("asx", "video/x-ms-asf");
            mime.Add("atom", "application/atom+xml");
            mime.Add("au", "audio/basic");
            mime.Add("avi", "video/x-msvideo");
            mime.Add("axs", "application/olescript");
            mime.Add("bas", "text/plain");
            mime.Add("bcpio", "application/x-bcpio");
            mime.Add("bin", "application/octet-stream");
            mime.Add("bmp", "image/bmp");
            mime.Add("c", "text/plain");
            mime.Add("cab", "application/octet-stream");
            mime.Add("calx", "application/vnd.ms-office.calx");
            mime.Add("cat", "application/vnd.ms-pki.seccat");
            mime.Add("cdf", "application/x-cdf");
            mime.Add("chm", "application/octet-stream");
            mime.Add("class", "application/x-java-applet");
            mime.Add("clp", "application/x-msclip");
            mime.Add("cmx", "image/x-cmx");
            mime.Add("cnf", "text/plain");
            mime.Add("cod", "image/cis-cod");
            mime.Add("cpio", "application/x-cpio");
            mime.Add("cpp", "text/plain");
            mime.Add("crd", "application/x-mscardfile");
            mime.Add("crl", "application/pkix-crl");
            mime.Add("crt", "application/x-x509-ca-cert");
            mime.Add("csh", "application/x-csh");
            mime.Add("css", "text/css");
            mime.Add("csv", "application/octet-stream");
            mime.Add("cur", "application/octet-stream");
            mime.Add("dcr", "application/x-director");
            mime.Add("deploy", "application/octet-stream");
            mime.Add("der", "application/x-x509-ca-cert");
            mime.Add("dib", "image/bmp");
            mime.Add("dir", "application/x-director");
            mime.Add("disco", "text/xml");
            mime.Add("dll", "application/x-msdownload");
            mime.Add("dll.config", "text/xml");
            mime.Add("dlm", "text/dlm");
            mime.Add("doc", "application/msword");
            mime.Add("docm", "application/vnd.ms-word.document.macroEnabled.12");
            mime.Add("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            mime.Add("dot", "application/msword");
            mime.Add("dotm", "application/vnd.ms-word.template.macroEnabled.12");
            mime.Add("dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template");
            mime.Add("dsp", "application/octet-stream");
            mime.Add("dtd", "text/xml");
            mime.Add("dvi", "application/x-dvi");
            mime.Add("dwf", "drawing/x-dwf");
            mime.Add("dwp", "application/octet-stream");
            mime.Add("dxr", "application/x-director");
            mime.Add("eml", "message/rfc822");
            mime.Add("emz", "application/octet-stream");
            mime.Add("eot", "application/octet-stream");
            mime.Add("eps", "application/postscript");
            mime.Add("etx", "text/x-setext");
            mime.Add("evy", "application/envoy");
            mime.Add("exe", "application/octet-stream");
            mime.Add("exe.config", "text/xml");
            mime.Add("fdf", "application/vnd.fdf");
            mime.Add("fif", "application/fractals");
            mime.Add("fla", "application/octet-stream");
            mime.Add("flr", "x-world/x-vrml");
            mime.Add("flv", "video/x-flv");
            mime.Add("gif", "image/gif");
            mime.Add("gtar", "application/x-gtar");
            mime.Add("gz", "application/x-gzip");
            mime.Add("h", "text/plain");
            mime.Add("hdf", "application/x-hdf");
            mime.Add("hdml", "text/x-hdml");
            mime.Add("hhc", "application/x-oleobject");
            mime.Add("hhk", "application/octet-stream");
            mime.Add("hhp", "application/octet-stream");
            mime.Add("hlp", "application/winhlp");
            mime.Add("hqx", "application/mac-binhex40");
            mime.Add("hta", "application/hta");
            mime.Add("htc", "text/x-component");
            mime.Add("htm", "text/html");
            mime.Add("html", "text/html");
            mime.Add("htt", "text/webviewhtml");
            mime.Add("hxt", "text/html");
            mime.Add("ico", "image/x-icon");
            mime.Add("ics", "application/octet-stream");
            mime.Add("ief", "image/ief");
            mime.Add("iii", "application/x-iphone");
            mime.Add("inf", "application/octet-stream");
            mime.Add("ins", "application/x-internet-signup");
            mime.Add("isp", "application/x-internet-signup");
            mime.Add("IVF", "video/x-ivf");
            mime.Add("jar", "application/java-archive");
            mime.Add("java", "application/octet-stream");
            mime.Add("jck", "application/liquidmotion");
            mime.Add("jcz", "application/liquidmotion");
            mime.Add("jfif", "image/pjpeg");
            mime.Add("jpb", "application/octet-stream");
            mime.Add("jpe", "image/jpeg");
            mime.Add("jpeg", "image/jpeg");
            mime.Add("jpg", "image/jpeg");
            mime.Add("js", "application/x-javascript");
            mime.Add("jsx", "text/jscript");
            mime.Add("latex", "application/x-latex");
            mime.Add("lit", "application/x-ms-reader");
            mime.Add("lpk", "application/octet-stream");
            mime.Add("lsf", "video/x-la-asf");
            mime.Add("lsx", "video/x-la-asf");
            mime.Add("lzh", "application/octet-stream");
            mime.Add("m13", "application/x-msmediaview");
            mime.Add("m14", "application/x-msmediaview");
            mime.Add("m1v", "video/mpeg");
            mime.Add("m3u", "audio/x-mpegurl");
            mime.Add("man", "application/x-troff-man");
            mime.Add("manifest", "application/x-ms-manifest");
            mime.Add("map", "text/plain");
            mime.Add("mdb", "application/x-msaccess");
            mime.Add("mdp", "application/octet-stream");
            mime.Add("me", "application/x-troff-me");
            mime.Add("mht", "message/rfc822");
            mime.Add("mhtml", "message/rfc822");
            mime.Add("mid", "audio/mid");
            mime.Add("midi", "audio/mid");
            mime.Add("mix", "application/octet-stream");
            mime.Add("mmf", "application/x-smaf");
            mime.Add("mno", "text/xml");
            mime.Add("mny", "application/x-msmoney");
            mime.Add("mov", "video/quicktime");
            mime.Add("movie", "video/x-sgi-movie");
            mime.Add("mp2", "video/mpeg");
            mime.Add("mp3", "audio/mpeg");
            mime.Add("mp4", "video/mp4");
            mime.Add("mpa", "video/mpeg");
            mime.Add("mpe", "video/mpeg");
            mime.Add("mpeg", "video/mpeg");
            mime.Add("mpg", "video/mpeg");
            mime.Add("mpp", "application/vnd.ms-project");
            mime.Add("mpv2", "video/mpeg");
            mime.Add("ms", "application/x-troff-ms");
            mime.Add("msg", "application/vnd.ms-outlook");
            mime.Add("msi", "application/octet-stream");
            mime.Add("mso", "application/octet-stream");
            mime.Add("mvb", "application/x-msmediaview");
            mime.Add("mvc", "application/x-miva-compiled");
            mime.Add("nc", "application/x-netcdf");
            mime.Add("nsc", "video/x-ms-asf");
            mime.Add("nws", "message/rfc822");
            mime.Add("ocx", "application/octet-stream");
            mime.Add("oda", "application/oda");
            mime.Add("odc", "text/x-ms-odc");
            mime.Add("ods", "application/oleobject");
            mime.Add("one", "application/onenote");
            mime.Add("onea", "application/onenote");
            mime.Add("onetoc", "application/onenote");
            mime.Add("onetoc2", "application/onenote");
            mime.Add("onetmp", "application/onenote");
            mime.Add("onepkg", "application/onenote");
            mime.Add("osdx", "application/opensearchdescription+xml");
            mime.Add("p10", "application/pkcs10");
            mime.Add("p12", "application/x-pkcs12");
            mime.Add("p7b", "application/x-pkcs7-certificates");
            mime.Add("p7c", "application/pkcs7-mime");
            mime.Add("p7m", "application/pkcs7-mime");
            mime.Add("p7r", "application/x-pkcs7-certreqresp");
            mime.Add("p7s", "application/pkcs7-signature");
            mime.Add("pbm", "image/x-portable-bitmap");
            mime.Add("pcx", "application/octet-stream");
            mime.Add("pcz", "application/octet-stream");
            mime.Add("pdf", "application/pdf");
            mime.Add("pfb", "application/octet-stream");
            mime.Add("pfm", "application/octet-stream");
            mime.Add("pfx", "application/x-pkcs12");
            mime.Add("pgm", "image/x-portable-graymap");
            mime.Add("pko", "application/vnd.ms-pki.pko");
            mime.Add("pma", "application/x-perfmon");
            mime.Add("pmc", "application/x-perfmon");
            mime.Add("pml", "application/x-perfmon");
            mime.Add("pmr", "application/x-perfmon");
            mime.Add("pmw", "application/x-perfmon");
            mime.Add("png", "image/png");
            mime.Add("pnm", "image/x-portable-anymap");
            mime.Add("pnz", "image/png");
            mime.Add("pot", "application/vnd.ms-powerpoint");
            mime.Add("potm", "application/vnd.ms-powerpoint.template.macroEnabled.12");
            mime.Add("potx", "application/vnd.openxmlformats-officedocument.presentationml.template");
            mime.Add("ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12");
            mime.Add("ppm", "image/x-portable-pixmap");
            mime.Add("pps", "application/vnd.ms-powerpoint");
            mime.Add("ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12");
            mime.Add("ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow");
            mime.Add("ppt", "application/vnd.ms-powerpoint");
            mime.Add("pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12");
            mime.Add("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
            mime.Add("prf", "application/pics-rules");
            mime.Add("prm", "application/octet-stream");
            mime.Add("prx", "application/octet-stream");
            mime.Add("ps", "application/postscript");
            mime.Add("psd", "application/octet-stream");
            mime.Add("psm", "application/octet-stream");
            mime.Add("psp", "application/octet-stream");
            mime.Add("pub", "application/x-mspublisher");
            mime.Add("qt", "video/quicktime");
            mime.Add("qtl", "application/x-quicktimeplayer");
            mime.Add("qxd", "application/octet-stream");
            mime.Add("ra", "audio/x-pn-realaudio");
            mime.Add("ram", "audio/x-pn-realaudio");
            mime.Add("rar", "application/octet-stream");
            mime.Add("ras", "image/x-cmu-raster");
            mime.Add("rf", "image/vnd.rn-realflash");
            mime.Add("rgb", "image/x-rgb");
            mime.Add("rm", "application/vnd.rn-realmedia");
            mime.Add("rmi", "audio/mid");
            mime.Add("roff", "application/x-troff");
            mime.Add("rpm", "audio/x-pn-realaudio-plugin");
            mime.Add("rtf", "application/rtf");
            mime.Add("rtx", "text/richtext");
            mime.Add("scd", "application/x-msschedule");
            mime.Add("sct", "text/scriptlet");
            mime.Add("sea", "application/octet-stream");
            mime.Add("setpay", "application/set-payment-initiation");
            mime.Add("setreg", "application/set-registration-initiation");
            mime.Add("sgml", "text/sgml");
            mime.Add("sh", "application/x-sh");
            mime.Add("shar", "application/x-shar");
            mime.Add("sit", "application/x-stuffit");
            mime.Add("sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12");
            mime.Add("sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide");
            mime.Add("smd", "audio/x-smd");
            mime.Add("smi", "application/octet-stream");
            mime.Add("smx", "audio/x-smd");
            mime.Add("smz", "audio/x-smd");
            mime.Add("snd", "audio/basic");
            mime.Add("snp", "application/octet-stream");
            mime.Add("spc", "application/x-pkcs7-certificates");
            mime.Add("spl", "application/futuresplash");
            mime.Add("src", "application/x-wais-source");
            mime.Add("ssm", "application/streamingmedia");
            mime.Add("sst", "application/vnd.ms-pki.certstore");
            mime.Add("stl", "application/vnd.ms-pki.stl");
            mime.Add("sv4cpio", "application/x-sv4cpio");
            mime.Add("sv4crc", "application/x-sv4crc");
            mime.Add("swf", "application/x-shockwave-flash");
            mime.Add("t", "application/x-troff");
            mime.Add("tar", "application/x-tar");
            mime.Add("tcl", "application/x-tcl");
            mime.Add("tex", "application/x-tex");
            mime.Add("texi", "application/x-texinfo");
            mime.Add("texinfo", "application/x-texinfo");
            mime.Add("tgz", "application/x-compressed");
            mime.Add("thmx", "application/vnd.ms-officetheme");
            mime.Add("thn", "application/octet-stream");
            mime.Add("tif", "image/tiff");
            mime.Add("tiff", "image/tiff");
            mime.Add("toc", "application/octet-stream");
            mime.Add("tr", "application/x-troff");
            mime.Add("trm", "application/x-msterminal");
            mime.Add("tsv", "text/tab-separated-values");
            mime.Add("ttf", "application/octet-stream");
            mime.Add("txt", "text/plain");
            mime.Add("u32", "application/octet-stream");
            mime.Add("uls", "text/iuls");
            mime.Add("ustar", "application/x-ustar");
            mime.Add("vbs", "text/vbscript");
            mime.Add("vcf", "text/x-vcard");
            mime.Add("vcs", "text/plain");
            mime.Add("vdx", "application/vnd.ms-visio.viewer");
            mime.Add("vml", "text/xml");
            mime.Add("vsd", "application/vnd.visio");
            mime.Add("vss", "application/vnd.visio");
            mime.Add("vst", "application/vnd.visio");
            mime.Add("vsto", "application/x-ms-vsto");
            mime.Add("vsw", "application/vnd.visio");
            mime.Add("vsx", "application/vnd.visio");
            mime.Add("vtx", "application/vnd.visio");
            mime.Add("wav", "audio/wav");
            mime.Add("wax", "audio/x-ms-wax");
            mime.Add("wbmp", "image/vnd.wap.wbmp");
            mime.Add("wcm", "application/vnd.ms-works");
            mime.Add("wdb", "application/vnd.ms-works");
            mime.Add("wks", "application/vnd.ms-works");
            mime.Add("wm", "video/x-ms-wm");
            mime.Add("wma", "audio/x-ms-wma");
            mime.Add("wmd", "application/x-ms-wmd");
            mime.Add("wmf", "application/x-msmetafile");
            mime.Add("wml", "text/vnd.wap.wml");
            mime.Add("wmlc", "application/vnd.wap.wmlc");
            mime.Add("wmls", "text/vnd.wap.wmlscript");
            mime.Add("wmlsc", "application/vnd.wap.wmlscriptc");
            mime.Add("wmp", "video/x-ms-wmp");
            mime.Add("wmv", "video/x-ms-wmv");
            mime.Add("wmx", "video/x-ms-wmx");
            mime.Add("wmz", "application/x-ms-wmz");
            mime.Add("wps", "application/vnd.ms-works");
            mime.Add("wri", "application/x-mswrite");
            mime.Add("wrl", "x-world/x-vrml");
            mime.Add("wrz", "x-world/x-vrml");
            mime.Add("wsdl", "text/xml");
            mime.Add("wvx", "video/x-ms-wvx");
            mime.Add("x", "application/directx");
            mime.Add("xaf", "x-world/x-vrml");
            mime.Add("xaml", "application/xaml+xml");
            mime.Add("xap", "application/x-silverlight-app");
            mime.Add("xbap", "application/x-ms-xbap");
            mime.Add("xbm", "image/x-xbitmap");
            mime.Add("xdr", "text/plain");
            mime.Add("xla", "application/vnd.ms-excel");
            mime.Add("xlam", "application/vnd.ms-excel.addin.macroEnabled.12");
            mime.Add("xlc", "application/vnd.ms-excel");
            mime.Add("xlm", "application/vnd.ms-excel");
            mime.Add("xls", "application/vnd.ms-excel");
            mime.Add("xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12");
            mime.Add("xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12");
            mime.Add("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            mime.Add("xlt", "application/vnd.ms-excel");
            mime.Add("xltm", "application/vnd.ms-excel.template.macroEnabled.12");
            mime.Add("xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template");
            mime.Add("xlw", "application/vnd.ms-excel");
            mime.Add("xml", "text/xml");
            mime.Add("xof", "x-world/x-vrml");
            mime.Add("xpm", "image/x-xpixmap");
            mime.Add("xps", "application/vnd.ms-xpsdocument");
            mime.Add("xsd", "text/xml");
            mime.Add("xsf", "text/xml");
            mime.Add("xsl", "text/xml");
            mime.Add("xslt", "text/xml");
            mime.Add("xsn", "application/octet-stream");
            mime.Add("xtp", "application/octet-stream");
            mime.Add("xwd", "image/x-xwindowdump");
            mime.Add("z", "application/x-compress");
            mime.Add("zip", "application/zip");

        }
        public static string getMimeFromFile(string filename)
        {
            if (!File.Exists(filename))
                throw new FileNotFoundException(filename + " not found");

            byte[] buffer = new byte[256];
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                if (fs.Length >= 256)
                    fs.Read(buffer, 0, 256);
                else
                    fs.Read(buffer, 0, (int)fs.Length);
            }
            try
            {
                System.UInt32 mimetype;
                FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
                System.IntPtr mimeTypePtr = new IntPtr(mimetype);
                string mime = Marshal.PtrToStringUni(mimeTypePtr);
                Marshal.FreeCoTaskMem(mimeTypePtr);
                return mime;
            }
            catch (Exception e)
            {
                return "unknown/unknown";
            }
        }


        public static string getMimeFromExtension(string extension)
        {
            if (extension.StartsWith("."))
            {
                extension = extension.Substring(1);
            }
            if (mime.ContainsKey(extension))
            {
                return mime[extension.ToLower()];
            }
            else
            {
                return "application/octet-stream";
            }
        }
    }
}
